package com.bzm.service.bzm.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bzm.common.entity.HttpCode;
import com.bzm.common.entity.JsonResult;
import com.bzm.common.utils.IpParser.IpInfo;
import com.bzm.common.utils.SerialNumberUtils;
import com.bzm.config.BzmGlobalConfig;
import com.bzm.config.redis.RedisConfig;
import com.bzm.mapper.bzm.BzmImUserMapper;
import com.bzm.mapper.file.SysFileInfoMapper;
import com.bzm.pojo.bzm.BzmImUser;
import com.bzm.pojo.file.SysFileInfo;
import com.bzm.service.bzm.BzmImUserService;

/**
 * <p>
 * bzm聊天用户 服务实现类
 * </p>
 *
 * @author hemiao
 * @since 2022-07-12
 */
@Service
public class BzmImUserServiceImpl extends ServiceImpl<BzmImUserMapper, BzmImUser> implements BzmImUserService {
	
	@Autowired
	private BzmImUserMapper bzmImUserMapper;
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	@Autowired
	private SysFileInfoMapper sysFileInfoMapper;
	@Value("${server.port}")
    private String serverPort;
	

	@Override
	public JsonResult<String> addInfo(BzmImUser user) {
		
		
		return null;
	}

	@Override
	public JsonResult<String> updateInfo(BzmImUser user) {
		
		
		return null;
	}

	@Override
	public JsonResult<List<BzmImUser>> selectUserList(BzmImUser user) {
		
		
		return null;
	}

	@Override
	public JsonResult<BzmImUser> login(String phone, String nickname, IpInfo info,String avatar) {
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(StringUtils.isBlank(phone)) {
			httpCode.setMsg("手机号不能为空");
			return new JsonResult<BzmImUser>(null, httpCode);
		}
		/*
		 * if(StringUtils.isBlank(nickname)) { httpCode.setMsg("昵称不能为空"); return new
		 * JsonResult<BzmImUser>(null, httpCode); }
		 */
		if(phone.length()!=11) {
			httpCode.setMsg("手机号格式错误");
			return new JsonResult<BzmImUser>(null, httpCode);
		}
		String serviceUrl = BzmGlobalConfig.domain_name;
		// 默认头像	
		String headImgUrl = serviceUrl+"/static/images/樱木花道001_38629.jpg";
		QueryWrapper<SysFileInfo> fileWrapper = new QueryWrapper<SysFileInfo>();
		fileWrapper.eq("file_number", avatar);
		SysFileInfo fileInfo = sysFileInfoMapper.selectOne(fileWrapper);
		if(fileInfo!=null) {
			headImgUrl = serviceUrl + fileInfo.getFileUrl();
		}
		QueryWrapper<BzmImUser> queryWrapper = new QueryWrapper<BzmImUser>();
		queryWrapper.eq("phone", phone);
		BzmImUser imUser = bzmImUserMapper.selectOne(queryWrapper);
		LocalDateTime dateTime = LocalDateTime.now();
		if(imUser==null) {
			String userId = SerialNumberUtils.createNumber("bzm_im", null);
			if(StringUtils.isBlank(nickname)) {
				nickname = "八爪猫bzm001";
			}
			// 新增
			imUser = new BzmImUser();
			imUser.setPhone(phone);
			imUser.setLoginTime(dateTime);
			imUser.setUserId(userId);
			imUser.setLoginStatus(1);
			imUser.setNickname(nickname);	
			imUser.setUsername(phone);	
			imUser.setCreateTime(dateTime);
			imUser.setIpCountry(info.getCountry());
			imUser.setIpCity(info.getCity());
			imUser.setIpProvince(info.getProvince());
			imUser.setHeadImgUrl(headImgUrl);
			bzmImUserMapper.insert(imUser);
		}else {
			String userId = imUser.getUserId();
			// 修改	
			imUser.setLoginStatus(1);
			if(StringUtils.isNotBlank(nickname)) {
				imUser.setNickname(nickname);
			}
			imUser.setUpdateTime(dateTime);
			imUser.setUpdateId(userId);
			imUser.setIpCountry(info.getCountry());
			imUser.setIpCity(info.getCity());
			imUser.setIpProvince(info.getProvince());
			imUser.setHeadImgUrl(headImgUrl);
			bzmImUserMapper.updateById(imUser);
		}
		return new JsonResult<BzmImUser>(imUser, HttpCode.SUCCESS);
	}

	@Override
	public JsonResult<BzmImUser> loginOut(String userId) {
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(StringUtils.isBlank(userId)) {
			httpCode.setMsg("用户不能为空");
			return new JsonResult<BzmImUser>(null, httpCode);
		}
		QueryWrapper<BzmImUser> queryWrapper = new QueryWrapper<BzmImUser>();
		queryWrapper.eq("user_id", userId);
		BzmImUser imUser = bzmImUserMapper.selectOne(queryWrapper);
		if(imUser==null) {
			httpCode.setMsg("用户不存在");
			return new JsonResult<BzmImUser>(null, httpCode);
		}
		imUser.setUpdateTime(LocalDateTime.now());
		imUser.setLoginStatus(2);
		bzmImUserMapper.updateById(imUser);
		// 清掉 redis缓存	
		redisTemplate.delete(RedisConfig.im_user_bind_key_redis+userId);
		return new JsonResult<BzmImUser>(null, HttpCode.SUCCESS);
	}

}
